package com.blb.web;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;

import com.blb.service.BaseService;
import com.blb.service.impl.BaseServiceImpl;
import com.blb.utils.UUIDUtils;
import com.blb.web.base.BaseServlet;

/**
 * @author Administrator
 *
 */
@MultipartConfig
@WebServlet("/person/*")
public class PersonServlet extends BaseServlet {
	
	private BaseService service = new BaseServiceImpl();
	
	private static final long serialVersionUID = 7060561389976928528L;

	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		super.forward(request, response, this.getClass());
	}

	/**
	 * 上传头像
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void uploadHead(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		File file = new File("D:\\img");
		if (!file.exists()) {
			file.mkdir();
		}
		
		Collection<Part> partList = request.getParts();
		if(partList.size() == 1){
            Part part = request.getPart("file");
            String header = part.getHeader("content-disposition");
            String suffix = getSuffix(header);
            String fileName = UUIDUtils.getId() + suffix;
            part.write(file.getPath() + "\\" + fileName);
            super.responseJson("{\"result\" : true, \"value\" : \""+fileName+"\"}");
        }
	}
	
	/**
	 * 获取后缀
	 * @param header
	 * @return
	 */
	private String getSuffix(String header) {
		return header.substring(header.lastIndexOf(".")).replaceAll("\"", "");
	}
	
	/**
	 * 修改用户资料
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	public void updateUserInfo(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String headImg = request.getParameter("head_img");
		String nickname = request.getParameter("nickname");
		String signature = request.getParameter("signature");
		String address = request.getParameter("address");
		String tel = request.getParameter("tel");
		String email = request.getParameter("email");
		String userId = request.getSession().getAttribute("user_id").toString();
		
		String sql = "UPDATE `t_user` SET `tel` = ?, `nickname` = ?, `head_img` = ?, `email` = ?, `address` = ?, `signature` = ? WHERE `id` = ?";
		try {
			service.execute(sql, tel, nickname, headImg, email, address, signature, userId);
			
			List<Map<String, Object>> list = service.get("select * from t_user where id = ?", userId);
			request.getSession().setAttribute("user", list.get(0));
			
			super.responseJson("{\"result\" : true}");
		} catch (SQLException e) {
			e.printStackTrace();
			super.responseJson("{\"result\" : false, \"msg\" : \"系统异常\"：" + e.getMessage() + "}");
		}
		
	}
}
